//
//  ZH_OrderTableViewCell.m
//  WisdomStar
//
//  Created by zhangliwen on 2018/10/19.
//  Copyright © 2018年 zhangliwen. All rights reserved.
//

#import "ZH_OrderTableViewCell.h"
#import "ZH_OrderCollectionViewCell.h"
@interface ZH_OrderTableViewCell()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
///内容
@property(nonatomic, weak) UICollectionView *collectionView;
///数据
@property(nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic,weak)UIView *backView;
@property (nonatomic,weak)UILabel *titlelabel;
@property (nonatomic,weak)UIImageView *leftImage;
@property (nonatomic,weak)UIButton *rightButton;
@property (nonatomic,weak)UIView *lineView;
@end
@implementation ZH_OrderTableViewCell

///创建
+(instancetype)cellWithTableView:(UITableView*)tableView
{
    static NSString *ID = @"ZH_OrderTableViewCell";
    ZH_OrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if(cell == nil){
        cell = [[ZH_OrderTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
}
///初始化
-(void)initDefault
{
    [self unShowClickEffect];
    //    self.bottomShortLineImage.hidden = NO;
}
- (void)loadSubviewConstraints{
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(FITHEIGHT(0));
        make.left.equalTo(self.contentView).offset(FITSCALE(12));
        make.right.equalTo(self.contentView).offset(FITSCALE(-12));
        make.height.mas_equalTo(FITSCALE(114));
        make.bottom.equalTo(self.contentView);
    }];
    [self.leftImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.backView).offset(FITSCALE(14));
        make.left.equalTo(self.backView).offset(FITSCALE(10));
        make.width.mas_equalTo(FITSCALE(13));
        make.height.mas_equalTo(FITSCALE(15));
    }];
    [self.titlelabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.backView).offset(FITSCALE(14));
        make.left.equalTo(self.leftImage.mas_right).offset(FITSCALE(7));
    }];
    [self.rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.titlelabel);
        make.right.equalTo(self.backView).offset(FITSCALE(-10));
    }];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.backView).offset(FITSCALE(44));
        make.left.right.equalTo(self.backView);
        make.height.mas_equalTo(FITHEIGHT(1));
    }];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lineView.mas_bottom).offset(FITHEIGHT(0));
        make.left.equalTo(self.backView).offset(FITWITH(0));
        make.right.equalTo(self.backView).offset(FITWITH(0));
        make.bottom.equalTo(self.backView).offset(FITHEIGHT(0));
    }];
    
}
///更新 ui
- (void)updateUiForData:(NSMutableArray *)datas
{
    if(datas.count > 0){
        [self.dataArray removeAllObjects];
        [self.dataArray addObjectsFromArray:datas];
        
        NSInteger item = self.dataArray.count;
        NSInteger rowNum = (item / 5) + ((item % 5) > 0 ? 1 : 0);
        
        CGFloat collectionHeight = FITSCALE(78) * rowNum + (rowNum * FITHEIGHT(0));
        
        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(collectionHeight);
        }];
        
        [self.collectionView reloadData];
    }else{
        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(HEIGHT_TRANSFORMATION(0));
        }];
    }
}

- (void)setLeftImages:(NSString *)leftImages{
    self.leftImage.image = [UIImage imageNamed:leftImages];
}
- (void)setLeftString:(NSString *)leftString{
    self.titlelabel.text = leftString;
}
- (void)setRightString:(NSString *)rightString{
    [self.rightButton setTitle:rightString forState:UIControlStateNormal];
}
#pragma mark -- UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.onButtonClick) {
        self.onButtonClick(indexPath.row);
    }
    [self.collectionView reloadData];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    CGSize size = CGSizeMake((kMainScreenWidth - FITSCALE(24)) / 5, FITSCALE(78));
    return size;
}

///设置行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return FITHEIGHT(0);
}

///设置列间距，配合item宽度来设置
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return WIDTH_TRANSFORMATION(0);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZH_OrderCollectionViewCell *cell = [ZH_OrderCollectionViewCell collectionView:collectionView cellForItemAtIndexPath:indexPath];
    if(self.dataArray.count > 0){
        [cell updateUiForIcon:self.dataArray[indexPath.row][0] andTitle:self.dataArray[indexPath.row][1]];
    }
    
    return cell;
}



#pragma mark -- getter,setter

///内容
- (UICollectionView *)collectionView
{
    if(!_collectionView){
        UICollectionView *collectionView = [UICollectionView createCollectionViewWithDelegateTarget:self forScrollDirection:UICollectionViewScrollDirectionVertical];
        //        collectionView.pagingEnabled = YES;
        [self.contentView addSubview:_collectionView = collectionView];
        //注册
        [ZH_OrderCollectionViewCell registerCollectionView:collectionView];
    }
    return _collectionView;
}


///数据
- (NSMutableArray *)dataArray
{
    if(!_dataArray){
        _dataArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _dataArray;
}

#pragma mark - getter,setter
- (UIView *)backView{
    if (!_backView) {
        UIView *view = [UIView createViewWithBackgroundColor:kFontColorWhite];
//        view.layer.borderWidth = 1.0f;
        view.layer.cornerRadius = FITSCALE(3);
        view.clipsToBounds = YES;
        view.userInteractionEnabled = YES;
//        view.layer.borderColor = [UIColor colorWithHexString:@"#FE1B53"].CGColor;
        [self.contentView addSubview:_backView = view];
    }
    return _backView;
}
- (UIImageView *)leftImage{
    if (!_leftImage) {
        UIImageView *image = [UIImageView createImageViewWithName:@""];
        [self.contentView addSubview:_leftImage = image];
    }
    return _leftImage;
}
- (UILabel *)titlelabel{
    if (!_titlelabel) {
        UILabel *label = [UILabel createLabelWithText:@"我的工具" font:kFontSize_28];
//        label.textColor = [UIColor colorWithHexString:@"#545454"];
        [self.contentView addSubview:_titlelabel = label];
    }
    return _titlelabel;
}
- (UIView *)lineView{
    if (!_lineView) {
        UIView *view = [UIView createViewWithBackgroundColor:[UIColor colorWithHexString:@"#EEEEEE"]];
        [self.contentView addSubview:_lineView = view];
    }
    return _lineView;
}
- (UIButton *)rightButton{
    if (!_rightButton) {
        UIButton *button = [UIButton createNoBgButtonWithTitle:@"" target:self action:nil];
        [button setTitleColor:[UIColor colorWithHexString:@"#999999"] forState:UIControlStateNormal];
        [self.contentView addSubview:_rightButton = button];
    }
    return _rightButton;
}
@end
